package com.hctforgreen.greenservice;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.taptwo.android.widget.MyXListView;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import cn.dns.reader.network.Util;

import com.hctforgreen.greenservice.ctr.HctBackTestJsonController;
import com.hctforgreen.greenservice.db.DBManager;
import com.hctforgreen.greenservice.model.LoginResultEntity;
import com.hctforgreen.greenservice.model.PwdEntity;
import com.hctforgreen.greenservice.ui.adapter.HisPwdListAdapter;
import com.hctforgreen.greenservice.ui.adapter.PwdListAdapter;
import com.hctforgreen.greenservice.utils.HctResult;
import com.hctforgreen.greenservice.utils.LoginResultStoreUtil;

/**
 * 展示开机密码
 * 
 * @author wuhs 20150714
 * 
 */
public class ShowPwdActivity extends ParentActivity {
	
	private DBManager mgr;
	private Activity context;
	private MyXListView xListView;
	private PwdListAdapter mPwdListAdapter;
	private TextView no_data_txt;
	private ArrayList<PwdEntity> pwdlist;
	private TextView view_titleTv;
	private ImageButton button_back;
	private Button login_GREEDAQ;
	private boolean isGREEDAQ;//判断是否需要跳转派工
	
	/**
	 * 登录标识，用于判断登录掌上通正式服务器的次数，超过1次则判断为正式服务器宕机
	 */
	public int pingCount = 0;
	
	/**
	 * false为正式服务器，true为云服务器
	 */
	private boolean isCloudServer = false;
	
	@Override
	protected void onResume() {
		super.onResume();
		initSkinLayout();
	}


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		mgr = new DBManager(context);
		setContentView(R.layout.activity_start_pwd_list);
		isGREEDAQ = getIntent().getBooleanExtra("isGREEDAQ",false);
		
		pingCount = getIntent().getIntExtra("PING_COUNT", 0);
		isCloudServer = getIntent().getBooleanExtra("WHICH_SERVER", false);
		/*if(!HctBackTestJsonController.isB_DEBUG()){
			Log.i("DATA", "传到MainActivityV2的--pingCount   " + pingCount);
			Log.i("DATA", "传到MainActivityV2的--isClounServer   " + isCloudServer);
		}*/
		
		findView();
		showResults();
		setListensers();
		
	}

	private void findView(){
		view_titleTv = (TextView) findViewById(R.id.tv_title);
		button_back = (ImageButton) findViewById(R.id.btn_back);
		no_data_txt = (TextView) findViewById(R.id.no_data_txt);
		login_GREEDAQ = (Button) findViewById(R.id.login_GREEDAQ);
		xListView = (MyXListView) findViewById(R.id.xListView);
	}
	
	
	private void showResults(){
		//view_titleTv.setText(getString(R.string.title_his_record));
		view_titleTv.setText("开机密码列表");
		pwdlist = (ArrayList<PwdEntity>)this.getIntent().getExtras().getSerializable("pwdlist");
		mPwdListAdapter = new PwdListAdapter(context, pwdlist, mOnClick, mgr, isCloudServer);
		xListView.setAdapter(mPwdListAdapter);
		if(isGREEDAQ){
			login_GREEDAQ.setVisibility(View.VISIBLE);
		}
		
	}
	
	private void setListensers(){
		button_back.setOnClickListener(mOnClick);
		login_GREEDAQ.setOnClickListener(mOnClick);
		xListView.setPullLoadEnable(false);
		xListView.setPullRefreshEnable(false);
	}
	
	
	
	
	private OnClickListener mOnClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_back:
				finish();
				break;
			case R.id.login_GREEDAQ://返回派工按钮
//				String packageName = "com.example.testdemo";
				String packageName = "com.gree.GreeAndroidApp.GreeAndroidApp";
				String className = "android.intent.action.GREEDAQ";
				
				if (!isInstalled(context, packageName)) {
					new AlertDialog.Builder(context)
							.setTitle(
									context.getString(R.string.dialog_default_title_hint))
							.setMessage(
									context.getString(R.string.dispath_download_and_install))
							.setNeutralButton(
									context.getString(R.string.dispath_download_and_install),
									new DialogInterface.OnClickListener() {
										@Override
										public void onClick(DialogInterface dialog,
												int which) {
											Util.dowloadApk(context,
													Util.DISPATCH_APK_DOWNLOAD_URL,
													"greeDispatch");// 下载格力派工apk,并安装

										}
									})
							.setNegativeButton(context.getString(R.string.cancel_hint),
									new DialogInterface.OnClickListener() {
										@Override
										public void onClick(DialogInterface dialog,
												int which) {
										}
									}).show();
					return;
				}

				try {
					Intent intent = new Intent();
					intent.setAction("android.intent.action.GREEDAQ");
					PwdEntity entity = pwdlist.get(0);
					
					Bundle bundle = new Bundle();
					bundle.putString("phone", LoginResultStoreUtil.get(ShowPwdActivity.this).phone);
					bundle.putString("innerBarcode", entity.stripeCode);
					bundle.putString("machinePassword", entity.openPassword);
					intent.putExtras(bundle);
					startActivity(intent);
					
					
					
					/*
					 * 20160623--390694编辑
					 * 增加派工批量获取开机密码
					 */					
					/*Intent intent = new Intent();
					intent.setAction("android.intent.action.GREEDAQ");
										
					Log.i("DATA", "pwdlist size   " + pwdlist.size());
					Bundle bundle = new Bundle();
					bundle.putString("phone", LoginResultStoreUtil.get(ShowPwdActivity.this).phone);
					
	
					if(pwdlist.size() > 1){						
						StringBuffer strPassword = new StringBuffer();
						for(int index = 0; index < pwdlist.size(); index++ ){
							String str = new String();
							str = pwdlist.get(index).stripeCode + "," + pwdlist.get(index).openPassword + ";";
							strPassword.append(str);
							Log.i("DATA", "密码拼接   " + strPassword.toString());
						}
						//Log.i("DATA", "密码拼接   " + strPassword.toString());
						bundle.putString("machinePassword", strPassword.toString());
						
					}else{
						PwdEntity entity = pwdlist.get(0);
						bundle.putString("innerBarcode", entity.stripeCode);
						bundle.putString("machinePassword", entity.openPassword);
					}					
					intent.putExtras(bundle);
					startActivity(intent);*/
					
					
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				break;
			default:
				break;
			}
		}

	};

	/**
	 * 判断应用是否已安装
	 * 
	 * @param context
	 * @param packageName
	 * @return
	 */

	private boolean isInstalled(Context context, String packageName) {
		final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
		List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
		if (pinfo != null) {
			for (int i = 0; i < pinfo.size(); i++) {
				String pn = pinfo.get(i).packageName;
				if (pn.equals(packageName))
					return true;
			}
		}
		return false;
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mgr.closeDB();
	}


}
